package com.yangaobo.mapper;

import com.yangaobo.dto.AssetsPageDTO;
import com.yangaobo.entity.Assets;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 杨奥博
 */
@Repository
public interface AssetsMapper {
    @Insert("""
            insert into rms_assets(`title`, `picture`, `price`, `price_unit`, `stock`, `stock_unit`, `total`, `info`, `deleted`, `version`, `created`, `updated`)
            values (#{title}, #{picture}, #{price}, #{priceUnit}, #{stock}, #{stockUnit}, #{total}, #{info}, #{deleted}, #{version}, #{created}, #{updated})
            """)
    int insert(Assets assets);

    @Select("""
            select * from rms_assets t
            where t.id = #{param1} and t.deleted = 0
            """)
    Assets select(Long id);

    @Select("""
            <script>
            select * from rms_assets t
            <where>
                <if test='title != null'> title like concat('%', #{title}, '%') and </if>
                t.deleted = 0
            </where>
            </script>
            """)
    List<Assets> list(AssetsPageDTO dto);

    @Update("""
            <script>
            update rms_assets
            <set>
                <if test="title != null">title = #{title},</if>
                <if test="picture != null">picture = #{picture},</if>
                <if test="price != null">price = #{price},</if>
                <if test="priceUnit != null">price_unit = #{priceUnit},</if>
                <if test="stock != null">stock = #{stock},</if>
                <if test="stockUnit != null">stock_unit = #{stockUnit},</if>
                <if test="total != null">total = #{total},</if>
                <if test="info != null">info = #{info},</if>
                <if test="deleted != null">deleted = #{deleted},</if>
                <if test="created != null">created = #{created},</if>
                <if test="updated != null">updated = #{updated},</if>
                version=version+1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """
            )
    int update(Assets assets);

    @Update("""
            update rms_assets set deleted = 1, updated = current_timestamp where id = #{id}
            """)
    int delete(Long id);

    @Update("""
            <script>
            update rms_assets set deleted = 1, updated = current_timestamp where
            <foreach collection='list' item='e' open='id in (' close= ')' separator=','>
                #{e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);

}
